# Build recipe for linux-libre (generic).
#
# Copyright (c) 2017-2018 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=linux
version=4.14.35
release=1

pkgname=kernel-generic

tarname=${program}-libre-${version}-gnu.tar.lz

# Remote source(s)
fetch=http://linux-libre.fsfla.org/pub/linux-libre/releases/${version}-gnu/$tarname

homepage=http://www.gnu.org/software/linux-libre

description="
The Linux kernel image and modules.

This is a generic configuration for the Linux kernel
which intends to run well in all environments.
"

license="GPLv2 only"

# To preserve 'srcdir' in order to produce "buildtree-generic", later
keep_srcdir=keep_srcdir

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Support LZIP

    patch -p1 < "${worktree}/patches/kernel/linux-4.14.21_lzip-1.diff"

    # Support AUFS

    cp -R -p "${worktree}/patches/kernel/aufs/Documentation"  \
             "${worktree}/patches/kernel/aufs/fs"             \
             .
    mkdir -p include/uapi/linux
    cp -p "${worktree}/patches/kernel/aufs/include/uapi/linux/aufs_type.h" \
          include/uapi/linux/

    # http://github.com/sfjro/aufs4-standalone/tree/aufs4.14
    patch -p1 < "${worktree}/patches/kernel/aufs/aufs4-kbuild.patch"
    patch -p1 < "${worktree}/patches/kernel/aufs/aufs4-base.patch"
    patch -p1 < "${worktree}/patches/kernel/aufs/aufs4-mmap.patch"
    patch -p1 < "${worktree}/patches/kernel/aufs/aufs4-standalone.patch"

    make mrproper

    case $arch in
    x86_64)
        echo "Copying config-x86_64_generic ..."
        cp -p "${worktree}/archive/kernel/config-x86_64_generic" .config
        kernel_arch=x86_64
        ;;
    i?86)
        echo "Copying config-x86_generic ..."
        cp -p "${worktree}/archive/kernel/config-x86_generic" .config
        kernel_arch=x86
        ;;
    *)
        echo "No custom configuration file detected for your architecture." 1>&2
        exit 1;
        ;;
    esac

    # Uncomment these lines for configuration purposes
    #echo "ABORTED for config"
    #exit 99

    make -j${jobs} bzImage
    make -j${jobs} modules
    make INSTALL_MOD_PATH="$destdir" modules_install

    # Install kernel images
    mkdir -p "${destdir}/boot"
    cp -p .config "${destdir}/boot/config"
    cp -p System.map "${destdir}/boot/System.map"
    cp -p "arch/${kernel_arch}/boot/bzImage" "${destdir}/boot/vmlinuz"

    # Insert post-install script manually

    # This will be recreated on the post-install,
    # we need to extract the kernel string for it
    rm -f "${destdir}"/lib/modules/${strver}/build \
          "${destdir}"/lib/modules/${strver}/source

    strver="$(ls -d "${destdir}"/lib/modules/*)"
    strver="${strver##*/}"

    mkdir -p "${destdir}/var/lib/qi"
    cat << EOF > "${destdir}/var/lib/qi/${full_pkgname}.sh"

# Kernel version
strver=$strver

# Generate or update the module dependency list
chroot . /sbin/depmod -a $strver

# Make generic symlinks for this kernel version (if needed)

srcdir="${srcdir##*/}"

(
    cd lib/modules/${strver} || exit 1
    rm -f build source
    ln -s /usr/src/${srcdir} build
    ln -s /usr/src/${srcdir} source
)

EOF
}

